
package cn.stronglink.asset.message.mq.dam.handle;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.plugins.Page;

import cn.stronglink.asset.message.mq.dam.entity.GetAssetListParamsEntity;
import cn.stronglink.asset.message.mq.dam.entity.IScreenCommandFactory;
import cn.stronglink.asset.message.mq.dam.entity.ScreenCommandEntity;
import cn.stronglink.asset.message.mq.dam.entity.dacai.UpdateArrayTextCommandFactory;
import cn.stronglink.asset.message.mq.dam.message.MQMessageOfDAM;
import cn.stronglink.asset.message.mq.topic.ReaderInfoSender;
import cn.stronglink.asset.module.assetmanage.itasset.vo.ItAssetVo;
import cn.stronglink.asset.module.dam.service.DamService;
import cn.stronglink.asset.module.dam.service.IDamService;
import cn.stronglink.suite.core.util.ContextUtils;

public abstract class AbstractAssetListPageHandle extends AbstractReturnExceptionHandle{
	protected final static String TOPIC_NAME = "serviceToDam";
	protected final static String ACTION_CODE = "screenRequest";
	protected final static String SEND_ACTION_CODE= "screenCommand";
	protected IDamService damService =(DamService)ContextUtils.getBean("sensorService");
	private ReaderInfoSender topicSender = (ReaderInfoSender) ContextUtils.getBean("readerInfoSender");
	private final static int COLUMN = 6;
	
	protected void sendCommandToDam(GetAssetListParamsEntity paramsEntity,Short screenId) throws Exception {
		Page<ItAssetVo> page =  damService.qryAssetListByRackCode(paramsEntity);
		if(page.getRecords()!=null&&page.getRecords().size()>0) {
			Map<Short,String> map = new HashMap<>();
			for(int i=0;i<paramsEntity.getPageSize();i++) {
				if(i>page.getRecords().size()-1) {
					map.put((short)(COLUMN*i+1), "");
					map.put((short)(COLUMN*i+2), "");
					map.put((short)(COLUMN*i+3), "");
					map.put((short)(COLUMN*i+4), "");
					map.put((short)(COLUMN*i+5), "");
					map.put((short)(COLUMN*i+6), "");
				}else {
					ItAssetVo itAssetVo = page.getRecords().get(i);
					map.put((short)(COLUMN*i+1), ((paramsEntity.getPageNum()-1)*9+i+1)+"");
					map.put((short)(COLUMN*i+2), String.valueOf(itAssetVo.getStartU()));
					map.put((short)(COLUMN*i+3), String.valueOf(itAssetVo.getEndU()));
					map.put((short)(COLUMN*i+4), itAssetVo.getRfidNo());
					map.put((short)(COLUMN*i+5), itAssetVo.getAssetName());
					map.put((short)(COLUMN*i+6), itAssetVo.getIp());
				}
				this.send(paramsEntity, screenId, map);
				map.clear();
			}
			map.put((short)996, String.valueOf(page.getCurrent()));
			map.put((short)998, String.valueOf(page.getCurrent()));
			this.send(paramsEntity, screenId, map);
			map.clear();
		}
	}
	
	private void send(GetAssetListParamsEntity paramsEntity,Short screenId,Map<Short,String> map) throws Exception {
		IScreenCommandFactory rackScreenCommandFactroy = new UpdateArrayTextCommandFactory(paramsEntity.getRackCode(), screenId, map);
		ScreenCommandEntity sc =  rackScreenCommandFactroy.createScreenCommand();
		
		MQMessageOfDAM message = new MQMessageOfDAM();
		message.setActioncode(SEND_ACTION_CODE);
		message.setAwsPostdata(sc);
		message.setTimestamp(System.currentTimeMillis());
		
		String json = JSON.toJSONString(message);
		topicSender.send(TOPIC_NAME,json);
		logger.info("topic:{}  发送命令:{}",TOPIC_NAME,json);
	}

}
